package deleteface.bank.jqsoft.cn.deleteface.ui;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import deleteface.bank.jqsoft.cn.deleteface.rsa.RsaHelper;
import deleteface.bank.jqsoft.cn.deleteface.setip.Constants;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

/**
 * 服务器交互类
 * 
 * @author gujunqi date：2014-1-1
 */
public class WebServiceUtils {
	// 定义webservice的命名空间
	public static String nameSpace = "http://tempuri.org/";

	// 定义webservice提供服务的url
	// public static String serviceURL =
	// "http://60.171.146.122:9098/WebService/AndrewsWebService.asmx";

	// 传入 调用 的方法 此方法不需传参
	public static String requestNoParams(String methodName, String serviceURL) {
		// 创建HttpTransportSE传输对象
		HttpTransportSE ht = new HttpTransportSE(serviceURL);
		try {
			ht.debug = true;
			// 使用SOAP1.1协议创建Envelop对象
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			// 实例化SoapObject对象
			SoapObject soapObject = new SoapObject(nameSpace, methodName);
			envelope.bodyOut = soapObject;
			// 设置与.NET提供的webservice保持较好的兼容性
			envelope.dotNet = true;

			// 调用webservice
			ht.call(nameSpace + methodName, envelope);
			if (envelope.getResponse() != null) {
				// 获取服务器响应返回的SOAP消息
				SoapObject object = (SoapObject) envelope.bodyIn;
				String result = object.getProperty(0).toString();
				return result;
			}
		} catch (SoapFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 需要传参类
	// 传入 调用的方法
	public static String requestByParams(String methodName, String strJson,
										 String serviceURL,String gkey) {
		Log.i("Info", "request:" + methodName + ":" + strJson);


		PublicKey publicKey = RsaHelper.decodePublicKeyFromXml( Constants.PUBLICKEY);
//     分段加密 新参数
		JSONObject encryptJsondata = new JSONObject();
		String userAccount="";

			userAccount="admin";

		try {
			encryptJsondata.put("businessId",gkey);
			encryptJsondata.put("serviceTypeId","4026AA324E5F4130BEE560AD888EE720");
			encryptJsondata.put("userAccount", userAccount);
			encryptJsondata.put("hosptalCode","");
			long timestamp= Calendar.getInstance().getTimeInMillis();
			String temp = timestamp + "";
			Log.d("timestamp", temp);
			encryptJsondata.put("timestamp", temp.substring(0, 10));
			encryptJsondata.put("offLineCall","0");
		} catch (JSONException e) {
			e.printStackTrace();
		}

		String str = RsaHelper.encryptDataFromStr(encryptJsondata.toString(), publicKey);



		// 创建httptransportSE传输对象
		HttpTransportSE ht = new HttpTransportSE(serviceURL);
		ht.debug = true;
		// 实例化SoapObject对象
		SoapObject soapObject = new SoapObject(nameSpace, methodName);
		// 添加一个请求参数
		// soapObject.addProperty("sLoginName", "admin");
		// soapObject.addProperty("sPassword", "123");
		soapObject.addProperty("strJson", strJson);
		soapObject.addProperty("encryptJson", str);

		// 使用soap1.1协议创建envelop对象
//		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
//				SoapEnvelope.VER10);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.bodyOut = soapObject;
		// 设置与.NET提供的webservice保持较好的兼容性
		envelope.dotNet = true;
		String result = null;
		// 调用webservice
		try {
			ht.call(nameSpace + methodName, envelope);
			if (envelope.getResponse() != null) {
				// 获取服务器响应返回的SOAP消息
				SoapObject object = (SoapObject) envelope.bodyIn;
				result = object.getProperty(0).toString();
				Log.i("Info", "response:" + methodName + ":" + result);
			}
		} catch (SoapFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.i("Info", "request:" + methodName + ":" + e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.i("Info", "request:" + methodName + ":" + e.getMessage());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.i("Info", "request:" + methodName + ":" + e.getMessage());
		}
		return result;
	}


	// 需要传参类
	// 传入 调用的方法
	public static String requestByParams(String methodName, String strJson,
			String serviceURL) {
		// 创建httptransportSE传输对象
		HttpTransportSE ht = new HttpTransportSE(serviceURL);
		ht.debug = true;
		// 实例化SoapObject对象
		SoapObject soapObject = new SoapObject(nameSpace, methodName);
		// 添加一个请求参数
		// soapObject.addProperty("sLoginName", "admin");
		// soapObject.addProperty("sPassword", "123");
		soapObject.addProperty("strJson", strJson);
		// 使用soap1.1协议创建envelop对象
//		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
//				SoapEnvelope.VER10);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.bodyOut = soapObject;
		// 设置与.NET提供的webservice保持较好的兼容性
		envelope.dotNet = true;

		// 调用webservice
		try {
			ht.call(nameSpace + methodName, envelope);
			if (envelope.getResponse() != null) {
				// 获取服务器响应返回的SOAP消息
				SoapObject object = (SoapObject) envelope.bodyIn;
				String result = object.getProperty(0).toString();
				Log.e("ArcSaveBitmapUrl",result);
				return result;
			}
		} catch (SoapFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}



	 public static String StringSplit(String value){
		if (!TextUtils.isEmpty(value)){
			String[] names = value.split("]\\*");
			for (int i = 0; i < names.length; i++) {
				System.out.println(names[i]);
			}
			return names[0] +"]";
		}
		return " ";
	 }

	public static String StringSplitgaoxueya(String value){
				if (!TextUtils.isEmpty(value)){
					String[] names = value.split("]\\*");
					for (int i = 0; i < names.length; i++) {
						System.out.println(names[i]);
					}
					return names[0]+"]" ;
				}
	return " ";
	}

	 public static String StringSplit2(String value){
		if (!TextUtils.isEmpty(value)){
			String[] names = value.split("]\\*");
			for (int i = 0; i < names.length; i++) {
				System.out.println(names[i]);
			}
			return names[1];
		}
		 return  " ";
	 }




}
